package org.smart4j.framework;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义ThreadLocal<br>
 *
 * @author andy
 * @version V1.0
 * @date 18/2/1 下午5:42
 */
public class MyThreadLocal<T> {

    private Map<Thread, T> contatiner = Collections.synchronizedMap(new HashMap<Thread, T>());

    public void set(T value){
        contatiner.put(Thread.currentThread(), value);
    }

    public T get(){
        Thread thread = Thread.currentThread();
        T value = contatiner.get(thread);
        if (value == null && !contatiner.containsKey(thread)){
            value = initialValue();
            contatiner.put(thread, value);
        }
        return value;
    }

    public void remove(){
        contatiner.remove(Thread.currentThread());
    }

    protected T initialValue(){
        return null;
    }

}
